package com.example.mybatisdemo.mapper;


import com.example.mybatisdemo.model.UserInfo;
import org.apache.ibatis.annotations.*;

import java.util.List;


@Mapper
public interface UserInfoMapper {



    @ResultMap(value = "Mapper")
    @Select("select * from userinfo")
    List<UserInfo> getUserInfoAll();

//    @Select("select * from userinfo where id = #{param1}")
//    List<UserInfo> getUserInfobyid(@Param("id") Integer userid);

    @Select("select * from userinfo where id = #{id}")
    UserInfo getUserInfobyid(@Param("id") Integer userid);

    @Select("select * from userinfo where gender = #{gender} and delete_flag = #{deleteFlag}")
    List<UserInfo> getUserInfobyidandname (Integer gender,Integer deleteFlag);

    @Options(useGeneratedKeys = true,keyProperty = "id")
    @Insert("insert into userinfo (username,password,age,gender) values " +
            "(#{username},#{password},#{age},#{gender})" )
    Integer insert(UserInfo userInfo);

    @Delete("delete from userinfo where id = #{id}")
    Integer delete(Integer id);

    @Update("update userinfo set username = #{userinfo.username}," +
            "age = #{userinfo.age},gender=#{userinfo.gender} where id = #{userinfo.id}")
    Integer update(@Param("userinfo") UserInfo userInfo);

    @Select("select id, username, password, age, gender, phone, delete_flag as deleteFlag, create_time as createTime, update_time as updateTime" +
            " from userinfo")
    List<UserInfo> selectUserAll1();



    @Results(id = "Mapper",value = {
        @Result(column = "delete_flag",property = "deleteFlag"),
        @Result(column = "create_time",property = "createTime"),
        @Result(column = "update_time",property = "updateTime")
    })
    @Select("select * from userinfo")
    List<UserInfo> selectUserAll2();


    @Select("select * from userinfo")
    List<UserInfo> selectUserAll3();



    /*
    * SQL注入演示
    * */
    @Select("select * from userinfo where username = #{userName}")
    List<UserInfo> selectUserByName(String userName);

    @Select("select * from userinfo where username = '${userName}'")
    List<UserInfo> selectUserByName2(String userName);


    @Select("select * from userinfo where username = '${name}' and password = '${password}'")
    UserInfo queryUserByNameAndPassword(String name, String password);

    @Select("select * from userinfo order by age ${order}")
    List<UserInfo> queryUserByOrder(String order);

//    @Select("select * from userinfo  where username like '%${name}%'")
//    List<UserInfo> queryUserByLike(String name);

    @Select("select * from userinfo  where username like concat('%','${name}','%')")
    List<UserInfo> queryUserByLike(String name);


    @Insert("<script>" +
            "insert into userinfo"+
            " <trim prefix=\"(\" suffix=\")\" suffixOverrides=\",\">\n" +
            "                <if test=\"username != null\">username,</if>\n" +
            "                <if test=\"password != null\">password,</if>\n" +
            "                <if test=\"age != null\">age,</if>\n" +
            "                <if test=\"gender != null\">gender</if>\n" +
            "            </trim>" +
            " values\n" +
            "        <trim prefix=\"(\" suffix=\")\" suffixOverrides=\",\">\n" +
            "            <if test=\"username != null\">#{username},</if>\n" +
            "            <if test=\"password != null\">#{password},</if>\n" +
            "            <if test=\"age != null\">#{age},</if>\n" +
            "            <if test=\"gender != null\">#{gender}</if>\n" +
            "        </trim>"+
            "</script>" )
    Integer insertByCondition(UserInfo userInfo);

}
